package com.xingxuecn.limit.service.impl;

import com.xingxuecn.limit.common.model.Page;
import com.xingxuecn.limit.common.model.TreeNode;
import com.xingxuecn.limit.common.model.User;
import com.xingxuecn.limit.dao.UserDao;
import com.xingxuecn.limit.dao.impl.UserDaoImpl;
import com.xingxuecn.limit.service.UserService;

import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

/**
 * @author 陈前钱 at 2018/9/10 0010 下午 3:26
 */
public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();

    @Override
    public List<User> findAll() throws SQLException {
        return userDao.findAll();
    }

    @Override
    public List<User> search(User user) throws SQLException {
        return userDao.search(user);
    }

    @Override
    public int add(User user) throws SQLException {
        return userDao.add(user);
    }

    @Override
    public int update(User user) throws SQLException {
        return userDao.update(user);
    }

    @Override
    public int delete(Integer id) throws SQLException {
        return userDao.delete(id);
    }

    @Override
    public List<TreeNode> userTree() throws SQLException {
        List<User> userList = userDao.findAll();
        //转成treeNode
        List<TreeNode> treeNodeList = new LinkedList<>();
        for (User user : userList) {
            TreeNode treeNode = new TreeNode(user.getId(),user.getName(),user.getUrl(),null);
            treeNodeList.add(treeNode);
        }

        List<TreeNode> result = TreeNode.buildTree(treeNodeList);
        return result;
    }

    @Override
    public Page searchPage(User user, Integer page, Integer rows) throws SQLException {
        return userDao.searchPage(user, page, rows);
    }
}
